<template>
  <!-- 过程上报 -->
  <div>
    <a-button v-if="editModel !== 'V'" class="mb12" icon="plus" type="primary" @click="handleAdd">添加</a-button>
    <a-table
      :columns="columns"
      row-key="id"
      :data-source="editModel === 'V' ? defaultList : dataList"
      :pagination="false"
      :loading="loading"
    >
      <!-- 时间 -->
      <template slot="time" slot-scope="text, record">
        <span v-if="editModel === 'V'">{{ text }}</span>
        <a-date-picker v-else v-model="record.time" placeholder="请选择时间" valueFormat="YYYY-MM-DD"></a-date-picker>
      </template>
      <!-- 地点 -->
      <template slot="address" slot-scope="text, record">
        <span v-if="editModel === 'V'">{{ text }}</span>
        <BaseInput v-else v-model="record.address" />
      </template>
      <!-- 负责人 -->
      <template slot="personInChargeId" slot-scope="text, record">
        <span v-if="editModel === 'V'">{{ record.personInChargeName }}</span>
        <SelectOrgPeople v-else init v-model="record.personInChargeId" />
      </template>
      <!-- 附件 -->
      <template slot="fileId" slot-scope="text, record">
        <template v-if="editModel !== 'V'">
          <l-table
            businessCode="UNION6"
            :uploadArrys.sync="record.fileInfoVOS"
            @fileArrysData="record.id && handleSubmit(record)"
          ></l-table>
        </template>
        <file-link v-else :fileList="record.fileInfoVOS" style="margin-top: -3px"></file-link>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record, index">
        <a @click="handleSubmit(record)">保存</a>
        <a-divider type="vertical"></a-divider>
        <a class="del_btn" @click="record.id ? handleRemove(record.id, getList) : dataList.splice(index, 1)">删除</a>
      </template>
    </a-table>
  </div>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import SelectOrgPeople from '@/components/Select/SelectOrgPeople' // 组织人员
import { inspCulturalProcessFile } from '@/api/union/workersCongress' // api

export default {
  name:'a'+ Date.now(),
  // name: 'ProcessEdit',
  components: {
    SelectOrgPeople,
  },
  extends: new BasePage(),
  data() {
    return {
      loading: false, // 表格loading
      dataList: [], // 列表
      removeApi: inspCulturalProcessFile,
    }
  },
  props: {
    // 请求接口
    api: Object,
    // 操作类型，当为V时只能查看
    editModel: {
      type: String,
      default: () => 'V',
    },
    // 默认列表
    defaultList: {
      type: Array,
      default: () => [],
    },
    // id
    id: [String, Number],
  },
  watch: {
    // 监听默认列表
    defaultList: {
      deep: true,
      handler(e) {
        this.dataList = e
      },
    },
  },
  computed: {
    // 表头
    columns() {
      let columns = [
        {
          title: '时间',
          width: 300,
          scopedSlots: { customRender: 'time' },
          dataIndex: 'time',
        },
        {
          title: '地点',
          width: 300,
          dataIndex: 'address',
          scopedSlots: { customRender: 'address' },
        },
        {
          title: '负责人',
          width: 300,
          dataIndex: 'personInChargeId',
          scopedSlots: { customRender: 'personInChargeId' },
        },
        {
          title: '附件',
          scopedSlots: { customRender: 'fileId' },
          dataIndex: 'fileId',
        },
      ]
      if (this.editModel !== 'V') {
        columns.push({
          title: '操作',
          dataIndex: 'action',
          width: '130px',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        })
      }
      return columns
    },
  },
  methods: {
    /**
     * 添加
     */
    handleAdd() {
      if(!this.dataList){
        this.dataList = []
      }
      this.dataList.push({})
    },
    /**
     * 获取列表
     */
    getList() {
      this.api
        .getDetail({ id: this.id })
        .then((res) => {
          this.loading = false
          this.dataList = res.data?.processFiles
        })
        .catch(() => {
          this.loading = false
        })
    },
    /**
     * 保存提交
     */
    handleSubmit(e) {
      let form = this.$clone(e)
      let name = form.id ? 'update' : 'create' // 有id为更新操作，否则为添加
      if (!form.time || !form.address || !form.personInChargeId) {
        this.$message.warning('信息不完整')
        return
      }
      this.loading = true
      // 格式化文件列表
      if (form.fileInfoVOS?.length > 0) {
        form.fileId = form.fileInfoVOS?.map((item) => item.commonId).join()
      }
      form.declareId = this.id
      inspCulturalProcessFile[name](form)
        .then(() => {
          this.$message.success('保存成功')
          this.getList()
        })
        .catch(() => {
          this.loading = false
        })
    },
  },
}
</script>
